package com.github.wxiaoqi.security.biz.modules.codex.api;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.wxiaoqi.security.biz.common.biz.request.MarketingFreezeReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingIntegralDetailListReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingIntegralDetailListRsp;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingReceiveReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingRefundReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingUnFreezeAndPayReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingUnFreezeReq;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.biz.modules.codex.biz.PersonalCouponRelationBiz;
import com.github.wxiaoqi.security.common.exception.BusinessException;
import com.github.wxiaoqi.security.common.msg.BaseApiResponse;

/**
 * 用户信息管理-外部
 * @author hexufeng
 *
 */
@Controller
@RequestMapping("marketingApi")
public class MarketingManageController extends BaseAction{
	
	private final static Logger logger = LoggerFactory.getLogger(MarketingManageController.class);
	
	@Autowired
	private PersonalCouponRelationBiz personalCouponRelationBiz;
	
	/**
	 * registered
	 * 红包、积分领取  营销中心调用crm完成领取
	 * @param marketingUnFreeze
	 * 
	 */
	@RequestMapping(value = "/marketing/receive", method = RequestMethod.POST)
	@ResponseBody
	public BaseApiResponse marketingReceive(@RequestBody @Valid MarketingReceiveReq params) throws Exception{
		/*String requestStr=request.getParameter("requestStr");
		MarketingReceiveReq2 params2=com.github.wxiaoqi.security.common.util.ParamStr2Bean.paramStr2Bean(requestStr, MarketingReceiveReq2.class);
		List<CouponInfo>couponList=new ArrayList<CouponInfo>();
		if(null!=params2.getCouponList()&&params2.getCouponList().size()>0){
		  for(Map<String, Object> map :params2.getCouponList()){
			  couponList.add(EntityUtils.map2Bean(map, CouponInfo.class));
		  }
		}
		MarketingReceiveReq params=new MarketingReceiveReq();
		params.setAcceptBizNo(params2.getAcceptBizNo());
		params.setMerNo(params2.getMerNo());
		params.setMerCustNo(params2.getMerCustNo());
		params.setBusinessType(params2.getBusinessType());
		params.setUnionId(params2.getUnionId());
		params.setCouponList(couponList);
		return custCouponRelationBiz.marketingReceive(params);*/
		//将json数组转成list
		//List<CouponInfo> list = JSONArray.toList((JSONArray)params.getCouponList(), new CouponInfo(), new JsonConfig());
		/*params.setAcceptBizNo(request.getParameter("acceptBizNo"));
		params.setMerCustNo(request.getParameter("merCustNo"));
		params.setMerNo(request.getParameter("merNo"));
		params.setBusinessType(request.getParameter("businessType"));
		params.setUnionId(request.getParameter("unionId"));
		//params.setCouponList((List<CouponInfo>)request.getParameter("couponList"));
		params.setIntegralAmount(request.getParameter("integralAmount"));*/
		BaseApiResponse baseApiResponse=new BaseApiResponse();
		try {
			baseApiResponse = personalCouponRelationBiz.marketingReceive(params);
		} catch (BusinessException e) {
			e.printStackTrace();
			baseApiResponse.setSuccess(false);
			baseApiResponse.setCode(BizResponseCode.INTEGRAL_RECEIVE_FAILED.getCode());
			baseApiResponse.setMessage(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			baseApiResponse.setSuccess(false);
			baseApiResponse.setCode(BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			baseApiResponse.setMessage(e.getMessage());
		}
		return baseApiResponse;
	}
	
	/**
	 * freeze
	 * 红包、积分冻结
	 * @param MarketingFreezeReq
	 * 
	 */
	@RequestMapping(value = "/marketing/freeze", method = RequestMethod.POST)
	@ResponseBody
	public BaseApiResponse marketingFreeze(@RequestBody @Valid MarketingFreezeReq params) throws Exception{
		BaseApiResponse baseApiResponse=new BaseApiResponse();
		try {
			baseApiResponse=personalCouponRelationBiz.marketingFreeze(params);
		} catch (BusinessException e) {
			e.printStackTrace();
			baseApiResponse.setSuccess(false);
			baseApiResponse.setCode(BizResponseCode.FREEZE_FAILED.getCode());
			baseApiResponse.setMessage(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			baseApiResponse.setSuccess(false);
			baseApiResponse.setCode(BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			baseApiResponse.setMessage(e.getMessage());
		}
		return baseApiResponse;
	}
	
	/**
	 * unfreeze
	 * 红包、积分解冻
	 * @param MarketingUnFreezeReq
	 * 
	 */
	@RequestMapping(value = "/marketing/unfreeze", method = RequestMethod.POST)
	@ResponseBody
	public BaseApiResponse marketingUnFreeze(@RequestBody @Valid MarketingUnFreezeReq params) throws Exception{
		//return personalCouponRelationBiz.marketingUnFreeze(params);
		BaseApiResponse baseApiResponse=new BaseApiResponse();
		try {
			baseApiResponse=personalCouponRelationBiz.marketingUnFreeze(params);
		} catch (BusinessException e) {
			e.printStackTrace();
			baseApiResponse.setSuccess(false);
			baseApiResponse.setCode(BizResponseCode.UNFREEZE_FAILED.getCode());
			baseApiResponse.setMessage(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			baseApiResponse.setSuccess(false);
			baseApiResponse.setCode(BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			baseApiResponse.setMessage(e.getMessage());
		}
		return baseApiResponse;
	}
	
	/**
	 * unfreezeAndPay
	 * 红包、积分解冻并出账
	 * @param MarketingUnFreezeAndPayReq
	 * 
	 */
	@RequestMapping(value = "/marketing/unfreezeAndPay", method = RequestMethod.POST)
	@ResponseBody
	public BaseApiResponse marketingUnFreezeAndPay(@RequestBody @Valid MarketingUnFreezeAndPayReq params) throws Exception{
//		return personalCouponRelationBiz.marketingUnFreezeAndPay(params);
		BaseApiResponse baseApiResponse=new BaseApiResponse();
		try {
			baseApiResponse=personalCouponRelationBiz.marketingUnFreezeAndPay(params);
		} catch (BusinessException e) {
			e.printStackTrace();
			baseApiResponse.setSuccess(false);
			baseApiResponse.setCode(BizResponseCode.UNFREEZE_FAILED.getCode());
			baseApiResponse.setMessage(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			baseApiResponse.setSuccess(false);
			baseApiResponse.setCode(BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			baseApiResponse.setMessage(e.getMessage());
		}
		return baseApiResponse;
	}
	
	/**
	 * marketingRefund
	 * 红包、积分退款
	 * @param MarketingRefundReq
	 * 
	 */
	@RequestMapping(value = "/marketing/marketingRefund", method = RequestMethod.POST)
	@ResponseBody
	public BaseApiResponse marketingRefund(@RequestBody @Valid MarketingRefundReq params) throws Exception{
		return personalCouponRelationBiz.marketingRefund(params);
	}
	
	/**
	 * integralDetail
	 * 积分明细查询
	 * @param
	 * 
	 */
	@RequestMapping(value = "/marketing/integralDetail", method = RequestMethod.POST)
	@ResponseBody
	public MarketingIntegralDetailListRsp integralDetail(@RequestBody @Valid MarketingIntegralDetailListReq params) throws Exception{
		return personalCouponRelationBiz.integralDetail(params);
	}
}
